/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectChangeManagerController
 * 项目经理变更
 *
 * 修改纪录
 * 2021-11-19 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2021-11-19
 */

package com.topscomm.mpm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.controller.auto.MpmProjectChangeManagerControllerAuto;
import com.topscomm.mpm.pojo.MpmProjectChangeManagerEntity;
import com.topscomm.mpm.pojo.MpmProjectChangeManagerInfoEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.IMpmProjectService;
import com.topscomm.mpm.service.impl.MpmProjectChangeManagerInfoService;
import com.topscomm.mpm.service.impl.MpmProjectPolymerizationService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

import javax.sql.rowset.serial.SerialException;

/**
 * @author: houyuhang
 * @date: 2021-11-19
 * @description:项目经理变更Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectChangeManager")
public class MpmProjectChangeManagerController extends MpmProjectChangeManagerControllerAuto implements IMpmProjectChangeManagerController {
	@Autowired
	private MpmProjectService mpmProjectService;
	@Autowired
	MpmProjectPolymerizationService mpmProjectPolymerizationService;
	@Autowired
	private MpmProjectChangeManagerInfoService mpmProjectChangeManagerInfoService;
	/**
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_usercode")) {
			String value = paraMap.get("qry_usercode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and usercode = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目经理变更申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}
	/**
	 *
	 * @description: 重写datacodelist
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		ResponseResult<Object> res = super.dataCodeList(paraMap);
		Map<String,Object> dataMap = (Map<String,Object>)res.getResult();
		List<Map<String, Object>> flowidList = flowDatacodeCache.getApprovalFlowListMap("ApprovalFlow_MpmProjectChangeManager", this.getUserBean(), "sortcode");
		dataMap.put("flowidList", flowidList);
		return ResponseResult.ok(dataMap);
	}
	/**
	 *
	 * @description: 查询该项目是否存在项目经理变更
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @modify:
	 */
	@GetMapping(value = "/getProjectChangeManager")
	public ResponseResult<Object> getProjectChangeManager(@RequestParam(required = false) Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled = 1 and usercode = '{0}' and state < 9",ConvertUtil.convertToString(paraMap.get("usercode")));
		List<MpmProjectChangeManagerEntity> mpmProjectChangeManagerEntities = this.service.queryByWhere(sql.toString());
		return ResponseResult.ok(mpmProjectChangeManagerEntities);
	}
	/**
	 *
	 * @description: 获取离职人员担任项目经理的项目
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2023-05-05
	 * @modify:
	 */
	@GetMapping(value = "/queryProjectCodeAndName")
	public ResponseResult<Object> queryProjectCodeAndName(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			// 判断是否为财总所在省人员，非本省人员不支持修改
			if (!this.service.checkSameProvinceStaff(paraMap)) {
				throw new ServiceException("只能交接本省区人员的工作，请确认！");
			}
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" projectmanagercode = '{0}' and projectcode is not null and projectstate not in ({1}) ",ConvertUtil.convertToString(paraMap.get("projectmanagercode")), MpmSystemConst.ProjectState.UnstartProjectCode + "," + MpmSystemConst.ProjectState.NormalFinish + "," + MpmSystemConst.ProjectState.AbnormalFinish);
			List<Map<String, Object>> res = mpmProjectPolymerizationService.queryProjectForChangeManager(sql.toString());
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-01-25
	 * @description: 项目交接详情表保存
	 * @modify:
	 */
	@Override
	protected void afterInsert(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.afterInsert(dataMap, paraMap);
		// 删除原内容，增加新内容
		long parentid = ConvertUtil.convertToLong(dataMap.get("id"));
		List<Map<String, Object>> infoList = (List<Map<String, Object>>) paraMap.get("managerinfo");
		mpmProjectChangeManagerInfoService.insertChangeManagerInfo(parentid, infoList);
	}
	/**
	 *
	 * @description: 检测是否为本省人员
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2023-05-05
	 * @modify:
	 */
	@GetMapping(value = "/checkSameProvinceStaff")
	public ResponseResult<Object> checkSameProvinceStaff(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			// 判断是否为财总所在省人员，非本省人员不支持修改
			if (!this.service.checkSameProvinceStaff(paraMap)) {
				throw new ServiceException("只能交接本省区人员的工作，请确认！");
			}
			return ResponseResult.ok("检验成功！");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

